package com.nextel.project.cobranza.mvc;

import com.nextel.project.Configuracion;
import com.nextel.project.cliente.ControlCliente;
import com.nextel.project.cobranza.ControlCobranza;
import com.nextel.project.cobranza.bean.CobranzaBean;
import com.nextel.project.reporte.mvc.validate.ValidateReporteForm;
import com.nextel.ui.OCompositeScreen;
import com.nextel.ui.OGrid;
import com.nextel.ui.OGridInfo;
import com.nextel.ui.OGridObjectRow;
import com.nextel.ui.OLabel;
import com.nextel.ui.OSoftKey;
import com.nextel.ui.OUILook;
//import javax.microedition.lcdui.Command;
import com.nextel.ui.custom.ODialogListener;
import javax.microedition.lcdui.Graphics;
import pe.com.nextel.idioma.IdiomaManager;
import pe.com.nextel.main.MVCAction;
import pe.com.nextel.rms.Persistente;
import pe.com.nextel.util.List;
import pe.com.nextel.util.math.MathUtil;

/**
 *
 * @author eespinoza
 */
public class MVCListaCobranza extends MVCAction {

    public OLabel texto;
    public OGrid table;
    private ValidateReporteForm vrf;
    private List lista;
    private boolean flagForm; // para que no presione dos veces al momento de buscar
    private OSoftKey cmdSeguir, cmdCancelar;

    public OGrid getTable() {
        return table;
    }

    public OLabel getString() {
        return texto;
    }

    public OCompositeScreen getScreen() {
        return formulario;
    }

    protected void destroy() {
        super.destroy();
        this.lista = null;

        if (table != null) {
            table.clear();

            table = null;
        }
    }

    protected void createView() {


        cmdCancelar = new OSoftKey(Configuracion.BOTON_ATRAS);
        cmdSeguir = new OSoftKey(Configuracion.BOTON_SEGUIR);

        formulario.setTitle(IdiomaManager.getInstance().getMensaje(IdiomaManager.JAVA_COBRANZA), OUILook.PLAIN_SMALL_TITLE);//"Cobranza"

        formulario.addSoftKey(cmdSeguir);
        formulario.addSoftKey(cmdCancelar);

    }

    protected void createItems() {
        //setTitulo() ;
        crearLista();
        flagForm = true;
    }

    private void crearLista() {

        int numberOfviewportLines = 5;
        int numberOfColumns = 5;
        
        String strDoc = " " + IdiomaManager.getInstance().getMensaje(IdiomaManager.JAVA_DOC) + " ";
        String strTotal = " " + IdiomaManager.getInstance().getMensaje(IdiomaManager.JAVA_TOTAL) + " ";
        String strCobr = " " + IdiomaManager.getInstance().getMensaje(IdiomaManager.JAVA_PAGO_COBR) + " ";
        String strSaldo = " " + IdiomaManager.getInstance().getMensaje(IdiomaManager.JAVA_SALDO) + " ";
        String strFecha = " " + IdiomaManager.getInstance().getMensaje(IdiomaManager.JAVA_FECHA) + " ";
        
        String columnNames[] = {strDoc, strTotal, strCobr, strSaldo, strFecha};
        
        int maxWidth = (int) (formulario.getWidth() * numberOfColumns / 3.0);
        int columnWidths[] = {maxWidth / numberOfColumns,
            maxWidth / numberOfColumns, maxWidth / numberOfColumns, maxWidth / numberOfColumns, maxWidth / numberOfColumns};
        int columnAlignments[] = {Graphics.LEFT, Graphics.HCENTER, Graphics.HCENTER, Graphics.LEFT, Graphics.LEFT};

        OGridInfo gridInfo = new OGridInfo(OGridInfo.GRID_BORDER_TYPE_BOTH, numberOfviewportLines,
                numberOfColumns,
                columnNames,
                columnAlignments, columnWidths, maxWidth,
                OUILook.PLAIN_SMALL_TEXT,
                OUILook.PLAIN_SMALL_TEXT, OUILook.PLAIN_SMALL_TEXT);

        table = new OGrid(gridInfo);
        vrf = new ValidateReporteForm(null);
        table.setTableListener(vrf);

        obtenerLista(numberOfColumns);
        table.setColumnHeadings(columnNames, OUILook.PLAIN_SMALL_TITLE);
        table.setViewableWidth(formulario.getWidth());

        formulario.add(table);


    }

    public void setTitulo() {
        formulario.setTitle(lista.size() + " "+IdiomaManager.getInstance().getMensaje(IdiomaManager.JAVA_COBRANZAS), OUILook.PLAIN_SMALL_TITLE);//Cobranzas
    }

    private void obtenerLista(int numberOfColumns) {

        try {
            CobranzaBean bean;

            lista = ControlCobranza.getInstance().listarCobranzarCliente(ControlCliente.getInstance().currentCliente.getCodigo());
//            System.out.println("Lista= " + lista);
//            System.out.println("-->Lista size:" + lista.size());
            if (lista != null) {

                OGridObjectRow items[] = new OGridObjectRow[lista.size()];

//                System.out.println("-->Lista size:" + String.valueOf(lista.size()));

                for (int i = 0; i < lista.size(); i++) {

                    bean = (CobranzaBean) lista.get(i);
                    OGridObjectRow row = new OGridObjectRow(numberOfColumns);
                    String saldo = MathUtil.restar(bean.getMontoTotal(), bean.getMontoPagado());
                    row.addCell(0, bean.getNumeroDocumento());
                    row.addCell(1, Configuracion.CambiarSimbolo(MathUtil.setScale(bean.getMontoTotal(), Configuracion.DECIMAL_PRESENTACION)));
                    row.addCell(2, Configuracion.CambiarSimbolo(MathUtil.setScale(bean.getMontoPagado(), Configuracion.DECIMAL_PRESENTACION)));
                    row.addCell(3, Configuracion.CambiarSimbolo(MathUtil.setScale(saldo,Configuracion.DECIMAL_PRESENTACION)));
                    row.addCell(4, bean.getFechaVencimiento());

                    items[i] = row;

                }

                table.populate(items);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    public Persistente escoger() {
        if (lista.size() > 0) {
            Object obj = lista.get(table.getSelectedIndex());
            return (Persistente) obj;
        } else {
            return null;
        }
    }

    public void performAction(OSoftKey c) {
        if (flagForm) {
            if (c == cmdSeguir) {
                flagForm = false;
                Persistente persistir = escoger();
                if (persistir == null) {
                    ControlCobranza.getInstance().showMensaje(IdiomaManager.getInstance().getMensaje(IdiomaManager.JAVA_NO_COBRANZAS)+" ", formulario);//"No se tiene cobranza "
                    flagForm = true;
                } else {
                    destroy();
                    ControlCobranza.getInstance().iniciarCobranza((CobranzaBean) persistir);
                }
            } else if (c == cmdCancelar) {
                flagForm = false;
                destroy();
                ControlCliente.getInstance().setCurrent(ControlCliente.getInstance().mvcClienteDetalle.showScreen());
            }
        }
    }
}
